package com.reg.nstreg;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.ballala.featurerandom.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TimeZone;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NO_HISTORY;
import static android.content.Intent.FLAG_RECEIVER_REPLACE_PENDING;

public class MainActivity extends AppCompatActivity {

    class MyBrowser extends WebViewClient {
        boolean webviewSuccess;

        MyBrowser(MainActivity arg1, com.reg.nstreg.MainActivity arg2) {
            this(arg1);
        }

        private MyBrowser(MainActivity arg1) {
            super();
            this.webviewSuccess = true;
        }

        public void onPageFinished(WebView arg2, String arg3) {
            super.onPageFinished(arg2, arg3);
            MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    if(webView.getTitle().toLowerCase().contains("500 - Internal server error.".toLowerCase())) {
                        webView.loadUrl(_url, headerMap);
                    }

                }
            });
            CookieSyncManager.getInstance().sync();
        }

        public void onPageStarted(WebView arg1, String arg2, Bitmap arg3) {
            super.onPageStarted(arg1, arg2, arg3);
        }

        public void onReceivedError(WebView arg1, WebResourceRequest arg2, WebResourceError arg3) {
            super.onReceivedError(arg1, arg2, arg3);
            this.webviewSuccess = false;
        }

        public boolean shouldOverrideUrlLoading(WebView arg2, WebResourceRequest arg3) {
            arg2.loadUrl(arg3.getUrl().toString(), MainActivity.this.headerMap);
            return true;
        }

        public boolean shouldOverrideUrlLoading(WebView arg2, String arg3) {
            arg2.loadUrl(arg3, MainActivity.this.headerMap);
            return true;
        }
    }

    String _url;
    Button btnRefresh;
    boolean doubleBackToExitPressedOnce;
    public static String fcm = "";
    HashMap headerMap;
    ImageView imgLock;
    LinearLayout llNoNetwork;
    ProgressBar progressBar;
    RelativeLayout rlUrlBar;
    String themColor;
    TextView txtHttps;
    TextView txtStatus;
    TextView txtUrl;
    WebView webView;

    static {
    }

    public MainActivity() {
        super();
//        this._url = "https://social-m3.com/hashtag/page.php";
        this._url = "https://social-m3.com/";
        this.themColor = "";
        this.doubleBackToExitPressedOnce = false;
        this.headerMap = new HashMap();
    }

    void call_back_script(String arg3, String arg4) {
        arg3 = "fun_input(\'" + arg3 + "\',\'" + arg4 + "\');";
        if(Build.VERSION.SDK_INT >= 19) {
            this.webView.evaluateJavascript(arg3, null);
        }
        else {
            WebView v4 = this.webView;
            v4.loadUrl("javascript:" + arg3);
        }
    }

    private void changeStatusBarColor(String arg3) {
        try {
            if(Build.VERSION.SDK_INT >= 21) {
                Window v0 = this.getWindow();
                v0.addFlags(0x80000000);
                v0.setStatusBarColor(Color.parseColor(arg3));
                this.getWindow().setNavigationBarColor(Color.parseColor(arg3));
            }

            return;
        }
        catch(Exception e) {
            return;
        }
    }

    public void onBackPressed() {
        this.call_back_script("go-back", "go-back");
        if(this.webView.canGoBack()) {
            this.webView.goBack();
        }
        else {
            if(this.doubleBackToExitPressedOnce) {
                super.onBackPressed();
            }

            this.doubleBackToExitPressedOnce = true;
            Toast.makeText(((Context)this), "برای خروج مجدد کلید برگشت را بزنید", Toast.LENGTH_LONG).show();
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    MainActivity.this.doubleBackToExitPressedOnce = false;
                }
            }, 2000);
        }
    }

    protected void onCreate(Bundle arg12) {
        super.onCreate(arg12);
        this.setContentView(R.layout.activity_nstreg_main);
        webView = findViewById(R.id.webview);
        SharedPreferences v0 = this.getSharedPreferences("Setting", 0);
        SharedPreferences.Editor v1 = v0.edit();
        String v2 = "fcm";
        String v3 = "";
        MainActivity.fcm = v0.getString(v2, v3);
        this.themColor = v0.getString("themColor", "#121849");
        this.getWindow().addFlags(0x80);
        String v0_1 = this.themColor;
        if(v0_1 != null && !v0_1.equals(v3)) {
            this.changeStatusBarColor(this.themColor);
        }

        v0_1 = Resources.getSystem().getConfiguration().locale.getLanguage();
        TimeZone v4 = TimeZone.getDefault();
//        MyFirebaseMessagingService.c = ((Context)this);
        net.web_userAgent = new WebView(((Context)this)).getSettings().getUserAgentString();
//        this.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            public void onRefresh() {
//                MainActivity.this.refresn();
//            }
//        });
//        this.webView = this.findViewById(0x7F0700BC);
//        this.rlUrlBar.setVisibility(8);
//        if(!FirebaseApp.getApps(((Context)this)).isEmpty()) {
//            FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener(v1) {
//                public void onComplete(Task arg3) {
//                    if(!arg3.isSuccessful()) {
//                        return;
//                    }
//
//                    MainActivity.fcm = arg3.getResult().getToken();
//                    this.val.shE.putString("fcm", MainActivity.fcm);
//                    this.val.shE.apply();
//                }
//            });
//        }

        int v6 = 21;
        if(Build.VERSION.SDK_INT >= v6) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(this.webView, true);
        }
        else {
            CookieManager.getInstance().setAcceptCookie(true);
        }

        this.webView.getSettings().setDefaultTextEncodingName("UTF-8");
        this.webView.getSettings().setJavaScriptEnabled(true);
        this.webView.getSettings().setAppCacheEnabled(true);
        this.webView.getSettings().setUseWideViewPort(true);
        this.webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        if(Build.VERSION.SDK_INT >= v6) {
            this.webView.getSettings().setMixedContentMode(0);
        }

        HashMap v5 = this.headerMap;
        v5.put("Accept-Tlme", v3 + System.currentTimeMillis());
        v5 = this.headerMap;
        StringBuilder v6_1 = new StringBuilder();
        StringBuilder v8 = new StringBuilder();
        v8.append(v3);
        v8.append(System.currentTimeMillis());
        v6_1.append(net.sha256(v8.toString()));
        v6_1.append(GZipper.mo);
        v5.put("Acept", v6_1.toString());
        this.headerMap.put("_v_", net.v);
        this.headerMap.put("pkg", "com.reg.nstreg");
        this.headerMap.put("version_app", "23");
        v5 = this.headerMap;
        v5.put("time_zone", v3 + v4.getDisplayName(false, 1));
        v5 = this.headerMap;
        v5.put("time_zone_short", v3 + v4.getDisplayName(false, 0));
        HashMap v12 = this.headerMap;
        v12.put("time_zone_data", v3 + v4.toString());
        v12 = this.headerMap;
        v12.put("device_lang", v3 + v0_1);
        v12 = this.headerMap;
        v12.put(v2, v3 + MainActivity.fcm);
        this.webView.setWebViewClient(new MyBrowser(this, null));
        this.webView.setWebChromeClient(new WebChromeClient() {
            public boolean onJsAlert(WebView arg20, String arg21, String arg22, JsResult arg23) {
                JSONObject v4 = null;
                String v0 = "refresh_web_enable";
                String v2 = "toolbar_show";
                String v3 = "themColor";
                boolean v6 = false;
                String v5 = null;
                try {
                    v4 = new JSONObject(arg22);
                    v5 = v4.getString("net_event");
                    v6 = v5.equals("net");
                }
                catch(JSONException v0_1) {
                }

                String v7 = "action";
                String v8 = "cookie";
                String v9 = "url";
                String v10 = "";
                if(v6) {
                    try {
                        String v6_1 = v4.getString(v9);
                        String v14 = v4.getString("method");
                        String v11 = v4.getString("parameters");
                        String v15 = v4.getString(v7);
                        net.cooDomain = v4.getString("coo_domain");
                        JSONArray v12 = v4.getJSONArray(v8);
                        JSONArray v13 = v4.getJSONArray("header");
                        String v20 = v0;
                        net v0_2 = new net();
                        arg21 = v15;
                        v6_1 = v10 + v6_1;
                        String v16 = v10 + v12.toString();
                        String v17 = v10 + v11;
                        v0_2.setNet(v6_1, v14, arg21, v16, v17, v10 + v13.toString());
                        v0_2.setOnCallBack(new net.CallBack() {
                            public void onFailure(String arg2, String arg3) {
                                call_back_script(arg2, arg3);
                            }

                            public void onResponse(int arg1, String arg2, final JSONObject arg3, String arg4) {
                                runOnUiThread(new Runnable() {
                                    public void run() {
                                        try {
                                            call_back_script(URLEncoder.encode(String.valueOf(getIntent().getData()), "UTF-8"), getIntent().getAction());
                                        }
                                        catch(UnsupportedEncodingException v0) {
                                            v0.printStackTrace();
                                        }
                                    }
                                });
                            }
                        });
//                        goto label_64;
//                        label_63:
//                        v20 = v0;
//                        label_64:
                        if(v5.equals("web")) {
                            v0 = v4.getString(v8);
                            v6_1 = v4.getString("user_agent");
                            v7 = v4.getString(v7);
                            v8 = v4.getString(v9);
                            WebControl v11_2 = new WebControl(MainActivity.this, v6_1, v7);
                            v11_2.setActiveUser(v7, v8, v0);
                            v11_2.setOnCallBack(new com.reg.nstreg.WebControl.CallBack() {
                                public void onFailure(String arg2, String arg3) {
                                   call_back_script(arg2, arg3);
                                }

                                public void onResponse(int arg1, String arg2, String arg3) {
                                    call_back_script(arg3, arg2);
                                }
                            });
                        }

                        if(!v4.isNull("star")) {
                            openAppRating();
                        }

                        if(!v4.isNull(v3)) {
                        }

                        if(!v4.isNull(v2)) {
                        }

                        v0 = v20;
                        if(!v4.isNull(v0)) {
                        }

                        if(v5.equals("open_link")) {
                            startActivity(new Intent("android.intent.action.VIEW", Uri.parse(v4.getString(v9))));
                        }

                    }
                    catch(JSONException v0_1) {
                    }
                }
                else {
//                    goto label_63;
                }

//                label_139:
                arg23.confirm();
                return true;
            }

            public void onProgressChanged(WebView arg4, int arg5) {
                String v0 = "";
                super.onProgressChanged(arg4, arg5);
//                try {
                    StringBuilder v1 = new StringBuilder();
                    v1.append(v0);
                    v1.append(arg4.getUrl());
                try {
                    URL v5 = new URL(v1.toString());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
//                    if(v5.getProtocol().toUpperCase().equals("https".toUpperCase())) {
//                        MainActivity.this.txtHttps.setVisibility(0);
//                        MainActivity.this.imgLock.setVisibility(0);
//                    }
//                    else {
//                        goto label_24;
//                    }
//
//                    label_31:
//                    TextView v4 = MainActivity.this.txtUrl;
//                    v4.setText("://" + v5.getHost());
//                    MainActivity.this.txtStatus.setText(((CharSequence)v0));
//                    return;
//                    label_24:
//                    MainActivity.this.txtHttps.setVisibility(8);
//                    MainActivity.this.imgLock.setVisibility(8);
//                    goto label_31;
//                }
//                catch(MalformedURLException ) {
//                    return;
//                }
            }

            public void onReceivedTitle(WebView arg2, String arg3) {
                super.onReceivedTitle(arg2, arg3);
                if(arg3.toLowerCase().indexOf("500 - Internal server error.".toLowerCase()) != -1) {
                    arg2.loadUrl(MainActivity.this._url, MainActivity.this.headerMap);
                }
            }
        });
        this.webView.loadUrl(this._url, this.headerMap);
    }

    public void openAppRating() {
        Object v5 = null;
//        new Handler().postDelayed(new Runnable() {
//            public void run() {
//                Intent v0 = new Intent(MainActivity.this, activity_market_helpRate.class);
//                v0.addFlags(0x48080000);
//                MainActivity.this.startActivity(v0);
//            }
//        }, 3000);
        StringBuilder v1 = new StringBuilder();
        v1.append("market://details?id=");
        String v2 = "com.reg.nstreg";
        v1.append(v2);
        String v3 = "android.intent.action.VIEW";
        Intent v0 = new Intent(v3, Uri.parse(v1.toString()));
        int v4 = 0;
        Iterator v1_1 = this.getPackageManager().queryIntentActivities(v0, 0).iterator();
        boolean label_45 = false;
        do {
            if(v1_1.hasNext()) {
                v5 = v1_1.next();
                if(!((ResolveInfo)v5).activityInfo.applicationInfo.packageName.equals("com.android.vending")) {
                    continue;
                }

                break;
            }

            label_45 = true;
            break;
        }
        while(true);

        if (!label_45) {

            ComponentName v4_1 = new ComponentName(((ResolveInfo) v5).activityInfo.applicationInfo.packageName, ((ResolveInfo) v5).activityInfo.name);
            v0.addFlags(FLAG_ACTIVITY_NEW_TASK);
            v0.addFlags(FLAG_RECEIVER_REPLACE_PENDING);
            v0.addFlags(FLAG_ACTIVITY_NO_HISTORY);
            v0.setComponent(v4_1);
            this.startActivity(v0);
            v4 = 1;
        }
        if(v4 == 0) {
            v1 = new StringBuilder();
            v1.append("https://play.google.com/store/apps/details?id=");
            v1.append(v2);
            this.startActivity(new Intent(v3, Uri.parse(v1.toString())));
        }
    }

    void refresn() {
        this.webView.loadUrl(this._url, this.headerMap);
    }
}

